package com.dong.classovertest;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class VideoTypeDao {
    public static void add () throws SQLException {
        String sql = "insert into video_types (type_name) values(?)";
        Connection connection = DBUtil.getConnection();
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,"搞笑");
        preparedStatement.executeUpdate();
        System.out.println("插入成功");
        connection.commit();
        connection.close();
    }

    public static void  delete() throws SQLException {
        String sql="delete from video_types where video_types.type_id=?";
        Connection connection = DBUtil.getConnection();
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,5);
        preparedStatement.executeUpdate();
        System.out.println("删除成功");
        connection.commit();
        connection.close();
    }
    public static void  update() throws SQLException {
        String sql="update video_types set type_name=? where type_id=?";
        Connection connection = DBUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,"喜剧");
        preparedStatement.setInt(2,1);
        preparedStatement.executeUpdate();
        System.out.println("更新完成");
        connection.commit();
        connection.close();

    }
    public static void query() throws SQLException {
        String sql="select *from video_types";
        Connection connection = DBUtil.getConnection();
        connection.setAutoCommit(false);
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            String type_id = resultSet.getString("type_id");
            String type_name = resultSet.getString("type_name");
            System.out.println(type_id+""+type_name);
        }
        connection.commit();
        connection.close();

    }

    public static void main(String[] args) throws SQLException {
//        VideoTypeDao.update();
//        VideoTypeDao.delete();
//        VideoTypeDao.add();
        VideoTypeDao.query();
    }

}

